package br.com.appclassificados.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.com.appclassificados.helpers.ConnectionFactory;
import br.com.appclassificados.model.Marca;

public class MarcaDao implements CRUD<Marca> {
	
	// TODO ATRIBUTOS
	private Connection con;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	
	// TODO Construtores
    public MarcaDao(){
    	super();
    	this.con = ConnectionFactory.getConnection().getConn();
    }	
	
    //Testado com Exito, 11:04, 19/10/2013, Maicon D.
	@Override
	public boolean insert(Marca obj) {
		try{
            PreparedStatement ps = this.con.prepareStatement("insert into marca values ( ? , ? )");
            ps.setInt(1, getChave());
            ps.setString(2, obj.getNome());
            ps.execute();
            ps.close();
            this.con.close();                         
            return true;            
        }catch(Exception e){
            return false;
        }   
	}

	//Testado com Exito, 11:04, 19/10/2013, Maicon D.
	//Erro corrigido na clausula WHERE 'id'.
	@Override
	public boolean delete(Marca obj) {
		try{
            PreparedStatement ps = this.con.prepareStatement("delete from marca where marcaid = ?");
            ps.setInt(1, obj.getMarcaId());
            ps.executeUpdate();
            ps.close();
            this.con.close();                         
            return true;            
        }catch(Exception e){
            return false;
        } 
	}

	//Testado com Exito, 11:04, 19/10/2013, Maicon D.
	@Override
	public boolean update(Marca obj) {
		try{
            PreparedStatement ps = this.con.prepareStatement("update marca set nome = ?  where marcaid = ?");            
            ps.setString(1, obj.getNome());
            ps.setInt(2, obj.getMarcaId());
            ps.executeUpdate();
            ps.close();
            this.con.close();                         
            return true;            
        }catch(Exception e){
            return false;
        }
	}

	//Testado com Exito, 11:04, 19/10/2013, Maicon D.
	@Override
	public List<Marca> readList() {
		try{
            PreparedStatement ps = this.con.prepareStatement("select marcaid, nome from marca");                                    
            this.rs = ps.executeQuery();            
            
            List<Marca> lm = new ArrayList<Marca>();
            
            while(this.rs.next()){
                Marca m = new Marca(this.rs.getInt("marcaid"), this.rs.getString("nome"));
                lm.add(m);                
            }
            return lm;
        }catch(Exception e){
            return null;
        }
	}

	//Testado com Exito, 11:04, 19/10/2013, Maicon D.
	@Override
	public Marca search(int id) {
		try{
            PreparedStatement ps = this.con.prepareStatement("select nome from marca where marcaid = ?");                        
            ps.setInt(1, id);
            this.rs = ps.executeQuery();
            
            if(this.rs.next()){
                Marca m = new Marca(id, this.rs.getString("nome"));
                return m;
            }else
                return null;
        }catch(Exception e){
            return null;
        }
	}

	//Testado com Exito, 11:04, 19/10/2013, Maicon D.
	@Override
	public Marca search(String value) {
		try{
	         PreparedStatement ps = this.con.prepareStatement("select marcaid, nome from marca where upper(nome) like ?");                        
	         ps.setString(1, "%"+value.toUpperCase()+"%");
	         this.rs = ps.executeQuery();
	            
	         if(this.rs.next()){
	             Marca m = new Marca(this.rs.getInt("marcaid"), this.rs.getString("nome"));
	             return m;
	         }else
	        	 return null;
	     }catch(Exception e){
	    	 return null;
	     }
	}

	@Override
	public int getChave() {
		try{
	         PreparedStatement ps = this.con.prepareStatement("select max(marcaid) from marca");                        
	         this.rs = ps.executeQuery();
	            
	         if(this.rs.next()){
	        	 return (this.rs.getInt("max(marcaid)") + 1);
	         }else
	        	 return 0;
	     }catch(Exception e){
	    	 return 0;
	     }
	}

}
